package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.constant.MqConst;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.service.RabbitService;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.product.service.ISkuInfoService;
import com.atguigu.ssyx.vo.product.SkuInfoQueryVo;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * sku信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/admin/product/skuInfo")
@RequiredArgsConstructor
//@CrossOrigin
@Api(tags = "商品管理")
public class SkuInfoController {

    private final ISkuInfoService skuInfoService;

    private final RabbitService rabbitService;


/*    url: `${api_name}/${page}/${limit}`,
    method: 'get',
    params: searchObj*/


    @ApiOperation("分页查询商品基本信息列表")
    @GetMapping("/{page}/{limit}")
    public Result list(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page,
                       @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                       @ApiParam(name = "skuInfoQueryVo", value = "查询对象", required = false) SkuInfoQueryVo skuInfoQueryVo
    ) {
        //1.创建分页对象
        Page<SkuInfo> p = new Page<>(page, limit);
        //2.创建分页条件
        LambdaQueryWrapper<SkuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(skuInfoQueryVo.getKeyword() != null && !skuInfoQueryVo.getKeyword().isEmpty(), SkuInfo::getSkuName, skuInfoQueryVo.getKeyword())
                .eq(skuInfoQueryVo.getCategoryId() != null, SkuInfo::getCategoryId, skuInfoQueryVo.getCategoryId())
                .eq(skuInfoQueryVo.getSkuType() != null, SkuInfo::getSkuType, skuInfoQueryVo.getSkuType());
        skuInfoService.page(p, wrapper);
        return Result.ok(p);


    }

  /*  url: `${api_name}/get/${id}`,
    method: 'get'*/

    @ApiOperation("根据id查询商品SKU信息")
    @GetMapping("/get/{id}")
    public Result get(@ApiParam(name = "id", value = "商品基本信息ID", required = true) @PathVariable Long id) {
        SkuInfoVo skuInfoVo = skuInfoService.getSkuInfoVo(id);
        return Result.ok(skuInfoVo);
    }


  /*  url: `${api_name}/save`,
    method: 'post',
    data: role*/

    @ApiOperation("新增商品基本信息")
    @PostMapping("/save")
    public Result save(@RequestBody SkuInfoVo skuInfo) {
        skuInfoService.saveSku(skuInfo);
        return Result.ok(null);
    }


 /*   url: `${api_name}/update`,
    method: 'put',
    data: role*/

    @ApiOperation("修改商品SKU信息")
    @PutMapping("/update")
    public Result update(@RequestBody SkuInfoVo skuInfoVo) {
        skuInfoService.updateSku(skuInfoVo);
        return Result.ok(null);
    }

    /* url: `${api_name}/remove/${id}`,
     method: 'delete'*/
    @ApiOperation("根据商品id删除商品SKU信息")
    @DeleteMapping("/remove/{id}")
    public Result remove(@ApiParam(name = "id", value = "商品ID", required = true) @PathVariable Long id) {
        skuInfoService.removeById(id);
        //删除sku信息的时候发送消息到mq同时同步到es里

        rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_DELETE, id);
        return Result.ok(null);
    }

/*    url: `${api_name}/batchRemove`,
    method: 'delete',
    data: idList*/

    @ApiOperation("批量删除商品SKU信息")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody Long[] idList) {
        skuInfoService.removeByIds(List.of(idList));

        return Result.ok(null);


    }

  /*  url: `${api_name}/check/${id}/${status}`,
    method: 'get'*/

    @ApiOperation("审核商品SKU信息")
    @GetMapping("/check/{id}/{status}")
    public Result check(
            @ApiParam(name = "id", value = "商品id", required = true) @PathVariable Long id,
            @ApiParam(name = "status", value = "商品状态", required = true) @PathVariable Integer status) {

        LambdaUpdateWrapper<SkuInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SkuInfo::getId, id)
                .set(SkuInfo::getCheckStatus, status);
        skuInfoService.update(wrapper);


        return Result.ok(null);
    }

    /*   url: `${api_name}/publish/${id}/${status}`,
       method: 'get'*/
    @ApiOperation("上架商品SKU信息")
    @GetMapping("/publish/{id}/{status}")
    public Result publish(
            @ApiParam(name = "id", value = "商品id", required = true) @PathVariable Long id,
            @ApiParam(name = "status", value = "商品是否上架", required = true) @PathVariable Integer status
    ) {
        LambdaUpdateWrapper<SkuInfo> wrapper = new LambdaUpdateWrapper<>();

        if (status == 1) {
            //代表上架
            wrapper.eq(SkuInfo::getId, id)
                    .set(SkuInfo::getPublishStatus, 1);
            skuInfoService.update(wrapper);

            //商品上架:   发送mq消息同步到es里
            //EXCHANGE_GOODS_DIRECT 交换机的名字 类型是direct
            //ROUTING_GOODS_UPPER 路由KEY的名字 _UPPER 代表上架
            rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_UPPER, id);

        } else {
            //代表下架
            wrapper.eq(SkuInfo::getId, id)
                    .set(SkuInfo::getPublishStatus, 0);
            skuInfoService.update(wrapper);

            //发送mq消息同步到es里
            rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT, MqConst.ROUTING_GOODS_LOWER, id);
        }

/*
        wrapper.eq(SkuInfo::getId, id)
                .set(SkuInfo::getPublishStatus, status);
        skuInfoService.update(wrapper);
*/


        return Result.ok(null);
    }




  /*  url: `${api_name}/isNewPerson/${id}/${status}`,
    method: 'get'*/

    @ApiOperation("新人专享商品SKU")
    @GetMapping("/isNewPerson/{id}/{status}")
    public Result isNewPerson(
            @ApiParam(name = "id", value = "商品id", required = true) @PathVariable Long id,
            @ApiParam(name = "status", value = "是否是新人", required = true) @PathVariable Integer status
    ) {
        LambdaUpdateWrapper<SkuInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SkuInfo::getId, id)
                .set(SkuInfo::getIsNewPerson, status);
        skuInfoService.update(wrapper);
        return Result.ok(null);

    }
}
